﻿// JScript 文件

var DeptTree = null;    //定义树名称

var mc = null;          //右键
var sUserName = "";     //系统登录用户名


/////////////////////////////////////////////////////DeptTree/////////////////////////////////////////////

//部门信息结构体

function deptItem(deptID,deptName,upperDept,createID,createDate,address,director,phone,fax,autoAnswer)
{
    this.deptID     = deptID;
    this.deptName   = deptName;
    this.upperDept  = upperDept;
    this.createID   = createID;
    this.createDate  = createDate;
    this.address    = address;
    this.director   = director;
    this.phone      = phone;
    this.fax        = fax;
    this.autoAnswer = autoAnswer;
}

//初始化部门根节点
function initDeptTree(userName)
{
    document.oncontextmenu = function(){return false;}
    sUserName = userName;
    DeptTree = new CtnTreeCtrl("DeptTree", false)
    DeptTree.create("DeptTree")	
    DeptTree.onExpand   = onExpand;
    DeptTree.onSelectItem = onSelectItem;
    DeptTree.onRightMenu  = onRightMenu;
    DeptTree.setIcons("../Images/treeimg", ['house.png']);
    
    var list;
    var xmlDoc = CtnTrans.getXml("/main.ashx?", "POST", "_method=DeptTree&Type=0&UserName=" + escape(userName)); 
    if (!CtnError.validate(xmlDoc))
    {
        CtnError.show();
        return;
    }
    var nodes = xmlDoc.selectNodes("NODES");
    var node = nodes[0].selectNodes("NODE");
    if (node.length == 0)
    {
        return;
    }
    list = getXmlList(xmlDoc);
    var vRoot = DeptTree.insertItem(list[0].deptName,list[0]);
    vRoot.icon = 0;
    DeptTree.show();
    onExpand(vRoot)
}

function getDeptAtt(deptID,type)
{
    var list;
    var xmlDoc = CtnTrans.getXml("/main.ashx?", "POST", "_method=GetDeptAtt&DeptID=" + escape(deptID)); 
    if (!CtnError.validate(xmlDoc))
    {
        CtnError.show();
        return;
    }
    list = getXmlList(xmlDoc);
    writeDeptInfo(type,list[0]);
    getUserList(list[0]);
}

//填充树结构体
function getXmlList(xmlDoc)
{
    var list = new Array();

	var nodes = xmlDoc.selectNodes("NODES");
	for(i = 0; i < nodes.length; i++)
	{
	   var node = nodes[i].selectNodes("NODE");
	   
	   for (k = 0; k < node.length; k++)
	   {  
	      var item = new deptItem("","","","","","","","","","");
	      item.deptID       = node[k].getAttribute("DeptId");
	      item.deptName     = node[k].getAttribute("DeptName"); 
	      item.upperDept    = node[k].getAttribute("UpperDept");
	      item.createID     = node[k].getAttribute("CreateId");
	      item.createDate    = node[k].getAttribute("CreateDate");
	      item.address      = node[k].getAttribute("Address"); 
	      item.director     = node[k].getAttribute("Director");
	      item.phone        = node[k].getAttribute("Phone");
	      item.fax          = node[k].getAttribute("Fax");
	      item.autoAnswer   = node[k].getAttribute("Autoanswer");
	      list.push(item);
	   }
	}
	return list;
}

//树节点展开函数
function onExpand(item)
{
    if(item.isdeal == false)
	{
        var list;
        var xmlDoc = CtnTrans.getXml("/main.ashx?", "POST", "_method=DeptTree&ExpendDeptId=" + escape(item.getItemData().deptID) + "&Type=1"); 
        if (!CtnError.validate(xmlDoc))
        {
            CtnError.show();
            return;
        }
        
        var nodes = xmlDoc.selectNodes("NODES");
        var node = nodes[0].selectNodes("NODE");

        
        list = getXmlList(xmlDoc);
        for(var i = 0;i < list.length;i++)
        {
             var vItem = item.insertItem(list[i].deptName,list[i]);
             vItem.icon = 0;
        }
        item.show();
    }
}

//选择树节点触发函数

function onSelectItem(Choose)
{
    if (Choose != null)
    {
        if (Choose.getItemData() != 0)
        {
            if (Choose.getItemData().type == 0)
            {
                return
            }
            else
            {
                var info = Choose.getItemData();
                parent.frames[1].writeDeptInfo(0,info);
                parent.frames[1].getUserList(info);
            }
        }
    }
}

//定义右键菜单
function onRightMenu(item, evt)
{
    var entid = item.idx;
    mc = new CtnMenuCtrl("mc");
    with(mc)
    {
        addEvent("添加", "parent.frames[0].editByType('0','" + entid + "','" + sUserName + "')");
        addEvent("修改", "parent.frames[0].editByType('1','" + entid + "','" + sUserName + "')");
        addEvent("删除", "parent.frames[0].editByType('2','" + entid + "','" + sUserName + "')");
    }
    mc.show(evt);
}

//根据不同右键菜单类型对树节点进行编辑
function editByType(type,entID,sUserName)
{
    var item = DeptTree.findItem(entID);
    if (type == 0)   //添加
    {
        parent.frames[1].addDept(item.data,sUserName);
    }
    else if (type == 1) //修改
    {
        parent.frames[1].updateDept(item.data,sUserName);
    } 
    else     //删除
    {
        //delDept(item.data,sUserName);
        top.CtnDlg.confirm("确定删除?", delDept, {info:item.data,userName:sUserName});
    }
}

//部门添加
function addDept(info,sUserName)
{
    $("UserRow").hide();
    $("ls").hide();
    $($("CreatDate").parentNode.parentNode).hide();     //将整一行屏蔽掉<tr></tr>
    $("DeptName").disabled = false;
    $("UpperDeptName").disabled = true;
    $("CreatorName").disabled = true;
    
    var objConfirm = $("Confirm");
    objConfirm.value = "保存";
    objConfirm.show();
    objConfirm.onclick = confirmEdit;
    objConfirm.operType = 0;
    objConfirm.info = info;
    objConfirm.userName = sUserName;
    
    $("DeptName").value = "";
    $("UpperDeptName").value = info.deptName;
    $("CreatorName").value = sUserName;
    
    $("DeptAddr").value = "";
    $("Director").value = "";
    $("Phone").value = "";
    $("Fax").value = "";
    $("AutoAnswer").value = "";
}

//部门修改
function updateDept(info,sUserName)
{
    $("UserRow").hide();
    $("ls").hide();
    $("DeptName").disabled = false;
    $("UpperDeptName").disabled = true;
    $("CreatorName").disabled = true;
    $("CreatDate").disabled = true;
    
    var objConfirm = $("Confirm");
    objConfirm.value = "修改";
    objConfirm.show();
    objConfirm.onclick = confirmEdit;
    objConfirm.operType = 1;
    objConfirm.info = info;
    objConfirm.userName = sUserName;
    writeDeptInfo(1,info,sUserName);
}

//部门删除
function delDept(flag, delparam)
{
    var info = delparam.info;
    var userName = delparam.userName;
    var data = "";
    var params = "";
    
    if (flag)
    {
        if (info.upperDept == "0")
        {
            top.CtnDlg.warning("该部门无法删除！");
            return;
        }
        params = "_method=GetCreatorName&CreatorID=" + info.createID;
        data = CtnTrans.getText("/main.ashx?","post", params);
        if (!CtnError.validate(data))
        {
            CtnError.show();
            return;
        }
        if (data != userName)
        {
            top.CtnDlg.warning("您无权对该部门执行删除操作！");
            return;
        }
        
        params = "_method=DeptDel&DeptID=" + info.deptID;
        data = CtnTrans.getText("/main.ashx?","post", params); 
        if (!CtnError.validate(data))
        {
            CtnError.show();
            return;
        }
        top.CtnDlg.msg("部门删除成功！");
        initDeptTree(userName);
    }
    else
    {
        return;
    }
}

//显示部门详细信息
function writeDeptInfo(type,info,userName)
{
    var data = "";
    var params = "";
    
    var objDept = $("DeptName");
    objDept.value = info.deptName;
    
    params = "_method=GetUDeptName&DeptID=" + info.upperDept;
    data = CtnTrans.getText("/main.ashx?","post", params);
    if (!CtnError.validate(data))
    {
        CtnError.show();
        return;
    }
    $("UpperDeptName").value = data;
    params = "_method=GetCreatorName&CreatorID=" + info.createID;
    data = CtnTrans.getText("/main.ashx?","post", params);
    if (!CtnError.validate(data))
    {
        CtnError.show();
        return;
    }
    
    $("CreatorName").value = data;
    var objDate = $("CreatDate");
    objDate.value = info.createDate;
    objDate.show();
    $("DeptAddr").value = info.address;
    $("Director").value = info.director;
    $("Phone").value = info.phone;
    $("Fax").value = info.fax;
    $("AutoAnswer").value = info.autoAnswer;
    var objConfirm = $("Confirm");
    if (type == 1)  //如果是执行修改操作，则要进行权限判断
    {
        if (data != userName)
        {
            top.CtnDlg.warning("您无权对该部门执行修改操作！");
            objConfirm.disabled = true;
            return;
        }
        else
        {
            objConfirm.disabled = false;
        }
        if (info.upperDept == "0")
        {
            objDept.disabled = true;
        }
        else
        {
            objDept.disabled = false;
        }
    }
    else
    {
        objConfirm.disabled = false;
    }
}

//确认编辑：保存

function confirmEdit()
{
    var type = this.operType;
    var info = this.info;
    var userName = this.userName;
    
    var data = "";
    var params = "";
    var paraData = "";
    var deptName = $("DeptName").value;
    
    //部门名称不能为空
    if (deptName == "")
    {
        top.CtnDlg.warning("部门名称不能为空！");
        return;
    }
    
    if (type == "0")    //增加
    {
        params = "_method=CheckDeptName&DeptName=" + escape(deptName);
        data = CtnTrans.getText("/main.ashx?","post", params);
        if (!CtnError.validate(data))
        {
            CtnError.show();
            return;
        }
        if (data != "True")
        {
            top.CtnDlg.msg("数据库中已存在该部门名称，请您重新命名！");
            return;
        }
        paraData = deptName;
        paraData += "," + info.deptID;
        paraData += "," + $("DeptAddr").value;
        paraData += "," + $("Director").value;
        paraData += "," + $("Phone").value;
        paraData += "," + $("Fax").value;
        paraData += "," + $("AutoAnswer").value;
        params = "_method=DeptAdd&DeptPara=" + escape(paraData);
        data = CtnTrans.getText("/main.ashx?","post", params);
        if (!CtnError.validate(data))
        {
            CtnError.show();
            return;
        }
        
        top.CtnDlg.msg("部门添加成功！");
    }
    else        //修改
    {
        if (deptName != info.deptName)
        {
            params = "_method=CheckDeptName&DeptName=" + escape(deptName);
            data = CtnTrans.getText("/main.ashx?","post", params);
            if (!CtnError.validate(data))
            {
                CtnError.show();
                return;
            }
            if (data != "True")
            {
                top.CtnDlg.msg("数据库中已存在该部门名称，请您重新命名！");
                return;
            }
        }
        paraData = info.deptID;
        paraData += "," + deptName;
        paraData += "," + info.upperDept;
        paraData += "," + info.createID;
        paraData += "," + info.createDate;
        paraData += "," + $("DeptAddr").value;
        paraData += "," + $("Director").value;
        paraData += "," + $("Phone").value;
        paraData += "," + $("Fax").value;
        paraData += "," + $("AutoAnswer").value;
        params = "_method=DeptModify&DeptPara=" + escape(paraData);
        data = CtnTrans.getText("/main.ashx?","post", params);
        if (!CtnError.validate(data))
        {
            CtnError.show();
            return;
        }
        
        top.CtnDlg.msg("部门修改成功！");
    }
    parent.frames[0].initDeptTree(userName);
}
/////////////////////////////////////////////////////DeptTree/////////////////////////////////////////////

///////////////////////////////////////////////////DeptUserList///////////////////////////////////////////
var lDeptID = 0;
var list = null;

//定义部门所包含用户列表
function getUserList(info)
{
    lDeptID = info.deptID;
    list = new CtnListCtrl("list");
    $("UserRow").show();
    $("DeptName").disabled = false;
    $("UpperDeptName").disabled = false;
    $("CreatorName").disabled = false;
    $("CreatDate").disabled = false;
    $("Confirm").hide();
    var lsDiv = $("ls")
    lsDiv.show();
    lsDiv.style.height = Math.max(document.documentElement.clientHeight, document.body.clientHeight) - $("dept_info").offsetHeight + "px";
    lsDiv.style.width = Math.max(document.documentElement.clientWidth, document.body.clientWidth) + "px";
    list.create("ls");
    list.bfill = 1;
    list.onGoPage = Qurey;
    list.goPage(1);
    
}

//查询部门所包含用户信息并列表展示

function Qurey(page)
{
    var params = "_method=GetDeptUser&DeptID=" + lDeptID;
    var xmlDoc = CtnTrans.getXml("/main.ashx?", "post", params);
    if (!CtnError.validate(xmlDoc))
    {
        CtnError.show();
        return;
    }
    list.insertNode(CtnXML.getRootNode(xmlDoc), 1);
    
    for (var i = 0; i < list.arrRow.length; i++)
    {
        list.getCell(i, 1).text = '<a href=\"javascript:top.ExecuteATool(top.m_vt.USER_USER,[0,' + list.getCell(i, 0).text + '])\">' + list.getCell(i, 1).text + '</a>';
    }
    listSize();
    list.show();
}

function listSize()
{
    //解决div比table宽的问题
    var tableWidth = 0;
    var count = 0;
    var lsDiv = $("ls");
    
    for (var i = 0; i < list.arrCol.length; i++)
    {
        if (list.arrCol[i].visible)
        {
            tableWidth += list.arrCol[i].maxsize;
            count ++;
        }
    }
    if (tableWidth < lsDiv.offsetWidth)
    {
        var offset = Math.floor((lsDiv.offsetWidth - tableWidth) / count);
        for (var i = 0; i < list.arrCol.length; i++)
        {
            if (list.arrCol[i].visible)
            {
                list.arrCol[i].maxsize += offset;
            }
        }
    }
}

///////////////////////////////////////////////////DeptUserList///////////////////////////////////////////







